package cn.iocoder.yudao.framework.excel.core.util;




import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义拦截器
 */
@Slf4j
public class BeautyStyleStrategy extends AbstractCellStyleStrategy {


    //这个是 easyexcel 的蓝色表头样式
    private WriteCellStyle headWriteCellStyle;
    //这个是 easyexcel 的左对齐内容样式
    private List<WriteCellStyle> contentWriteCellStyleListLeft;
    //这个是 easyexcel 的居中对其内容样式
    private List<WriteCellStyle> contentWriteCellStyleListCenter;

    //这个是 poi 的表头样式，中间会有一步转换
    private CellStyle headCellStyle;
    //这个是 poi 的左对齐内容样式，中间会有一步转换
    private List<CellStyle> contentCellStyleListLeft;
    //这个是 poi 的居中对齐内容样式，中间会有一步转换
    private List<CellStyle> contentCellStyleListCenter;

    //斑马纹构造方法
    public BeautyStyleStrategy(WriteCellStyle headWriteCellStyle,
                               List<WriteCellStyle> contentWriteCellStyleListLeft,List<WriteCellStyle> contentWriteCellStyleListCenter) {
        this.headWriteCellStyle = headWriteCellStyle;
        this.contentWriteCellStyleListLeft = contentWriteCellStyleListLeft;
        this.contentWriteCellStyleListCenter = contentWriteCellStyleListCenter;
    }
    //统一样式的构造方法
    public BeautyStyleStrategy(WriteCellStyle headWriteCellStyle,
                               WriteCellStyle contentWriteCellStyleLeft, WriteCellStyle contentWriteCellStyleCenter) {
        this.headWriteCellStyle = headWriteCellStyle;
        contentWriteCellStyleListLeft = new ArrayList<>();
        if (contentWriteCellStyleLeft != null){
            contentWriteCellStyleListLeft.add(contentWriteCellStyleLeft);
        }
        contentWriteCellStyleListCenter = new ArrayList<>();
        if (contentWriteCellStyleCenter != null){
            contentWriteCellStyleListCenter.add(contentWriteCellStyleCenter);
        }
    }

    //实例化后进行 easyexcel -> poi 样式的转换
//    @Override
//    protected void initCellStyle(Workbook workbook) {
//        if (headWriteCellStyle != null) {
//
//            headCellStyle = StyleUtil.buildCellStyle(workbook, headWriteCellStyle);
//        }
//        if (contentWriteCellStyleListLeft != null && !contentWriteCellStyleListLeft.isEmpty()) {
//            contentCellStyleListLeft = new ArrayList<CellStyle>();
//            for (WriteCellStyle writeCellStyle : contentWriteCellStyleListLeft) {
//                if (writeCellStyle == null){
//                    continue;
//                }
//                contentCellStyleListLeft.add(StyleUtil.buildCellStyle(workbook, writeCellStyle));
//            }
//        }
//        if (contentWriteCellStyleListCenter != null && !contentWriteCellStyleListCenter.isEmpty()) {
//            contentCellStyleListCenter = new ArrayList<CellStyle>();
//            for (WriteCellStyle writeCellStyle : contentWriteCellStyleListCenter) {
//                if (writeCellStyle == null){
//                    continue;
//                }
//                contentCellStyleListCenter.add(StyleUtil.buildCellStyle(workbook, writeCellStyle));
//            }
//        }
//    }

    //设置表头样式
    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        int colIndex = cell.getColumnIndex();
        //同样，根据不同的列编号选择使用不同的内容样式
        if (headCellStyle == null) {
            return;
        }
        cell.setCellStyle(headCellStyle);
    }

    //设置内容样式
    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        int rowIndex = cell.getRowIndex();
        //同样，根据不同的列编号选择使用不同的内容样式
//        if (rowIndex > 0) {
//            if (contentCellStyleListCenter == null || contentCellStyleListCenter.isEmpty()) {
//                return;
//            }
//            cell.setCellStyle(contentCellStyleListCenter.get(relativeRowIndex % contentCellStyleListCenter.size()));
//        }

        if (contentCellStyleListCenter != null && !contentCellStyleListCenter.isEmpty()) {
            cell.setCellStyle(contentCellStyleListCenter.get(0));
        }else if(contentCellStyleListLeft != null && !contentCellStyleListLeft.isEmpty()){
            cell.setCellStyle(contentCellStyleListLeft.get(0));
        }

    }



}


